package com.tour.modules.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tour.common.utils.PageUtils;
import com.tour.common.utils.Query;
import com.tour.common.utils.R;
import com.tour.modules.sys.shiro.ShiroUtils;
import com.tour.modules.web.entity.TWorkOrderEntity;
import com.tour.modules.web.service.TWorkOrderService;
import com.tour.modules.web.until.CalendarUtil;
import com.tour.modules.web.vo.TWorkOrderVo;

/**
 * 留言表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-01-06 22:44:15
 */
@RestController
@RequestMapping("tworkorder")
public class TWorkOrderController {
	@Autowired
	private TWorkOrderService tWorkOrderService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);

		List<TWorkOrderEntity> tWorkOrderList = tWorkOrderService.queryList(query);
		int total = tWorkOrderService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(tWorkOrderList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id) {
		TWorkOrderEntity tWorkOrder = tWorkOrderService.queryObject(id);

		return R.ok().put("tWorkOrder", tWorkOrder);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TWorkOrderEntity tWorkOrder) {
		tWorkOrderService.save(tWorkOrder);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody TWorkOrderEntity tWorkOrder) {
		tWorkOrderService.update(tWorkOrder);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] ids) {
		tWorkOrderService.deleteBatch(ids);

		return R.ok();
	}

	@RequestMapping("/listAll")
	public R listAll(@RequestParam Map<String, Object> params) throws ParseException {

		String date;

		if (params.get("date") == null) {
			date = null;
		} else {
			date = params.get("date").toString();
		}
		Long userId = ShiroUtils.getUserId();
		// 查询列表数据
		Query query = new Query(params);

		// 赋搜索关键词
		if (params.get("search") == null) {
			query.put("search", null);
		} else {
			query.put("search", params.get("search"));
		}

		int j = 0;
		if (j >= 1) {
			query.put("userId", null);
		} else {
			query.put("userId", userId);
		}

		if (date == null || date == "") {
			query.put("starttime", null);
			query.put("endtime", null);
		} else if (date.equals("all")) {
			query.put("starttime", null);
			query.put("endtime", null);
		} else if (date.equals("now")) {
			query.put("starttime", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			query.put("endtime", new SimpleDateFormat("yyyy-MM-dd ").format(CalendarUtil.getAfterDay(new Date(), 1)));
		} else if (date.equals("yesterday")) {
			query.put("starttime", new SimpleDateFormat("yyyy-MM-dd").format(CalendarUtil.getAfterDay(new Date(), -1)));
			query.put("endtime", new SimpleDateFormat("yyyy-MM-dd ").format(new Date()));
		} else {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date d = format.parse(params.get("endtime").toString());
			//
			query.put("starttime", params.get("starttime"));

			query.put("endtime", new SimpleDateFormat("yyyy-MM-dd ").format(CalendarUtil.getAfterDay(d, 1)));

		}

		List<TWorkOrderVo> tWorkOrderVos = tWorkOrderService.queryListAll(query);
		int total = tWorkOrderService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(tWorkOrderVos, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 查询一条详细信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/oneInfo/{id}")
	public R oneInfo(@PathVariable("id") Long id) {
		TWorkOrderVo tWorkOrderEntity = tWorkOrderService.queryObjectVo(id);

		return R.ok().put("tWorkOrderEntity", tWorkOrderEntity);
	}

}
